/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fiu.accesslog.filter;

import edu.fiu.accesslog.SingleAccessLog;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Hasan Mahmud @date Jul 6, 2012
 */
public class IPFilters extends Filters
{

        private static IPFilters singletonInstance = null;
        private HashMap<String, String> simpleFilteredIPs;
        private ArrayList<String> regFilteredIPs;

        public static synchronized IPFilters getInstance()
        {
                if (singletonInstance == null)
                {
                        singletonInstance = new IPFilters();
                }

                return singletonInstance;
        }

        protected IPFilters()
        {
                super();
                simpleFilteredIPs = new HashMap<String, String>(200);
                regFilteredIPs = new ArrayList<String>(20);
        }

        public void addSimpleIPforFilter(String ip)
        {
                simpleFilteredIPs.put(ip, null);
        }
        
        public void addRExpressedIPforFilter(String ip)
        {
                regFilteredIPs.add(ip);
        }

        @Override
        public boolean isFiltered(SingleAccessLog saLog)
        {
               
                String remoteHostName = saLog.getRemoteHostName();
                if (simpleFilteredIPs.containsKey(remoteHostName))
                {
                        return true;
                }
                
                for (String fIP:regFilteredIPs)
                {
                       if( remoteHostName.matches(fIP))
                       {
                               return true;
                       }
                }


                
                return false;
        }
}
